Server side Validation
Posted on: February 14, 2011 at 12:00 AM
In this tutorial you will learn how to validate data on server coming from client

Server Side Validation

It is very important to validate the data coming from the client side, so that wrong data could not process into the application.

In Struts you can validate the data as follows,

Write a simple login.jsp file asADS_TO_REPLACE_1

login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:head />
<center>
<h1>Please Login</h1>
<s:form action="register">
<s:textfield name="userName" key="userName" label="User Name"></s:textfield>
<s:textfield name="password" key="password" label="Password"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form></center>

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<h1>Welcome <s:property value="userName"/></h1>
</body>
</html>

Write a Simple Model for login application

LoginModel.javaADS_TO_REPLACE_2

package net.roseindia.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class LoginModel implements Serializable {

	private static final long serialVersionUID = 1L;

	private String userName;
	private String password;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String displayForm(){
	return "input";	
	}

}

Then Write an Action class for application

LoginValidation.java

package net.roseindia.action;

import java.util.ArrayList;
import java.util.List;

import net.roseindia.model.LoginModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginValidation extends ActionSupport implements
		ModelDriven<LoginModel> {

	private static final long serialVersionUID = 1L;
	LoginModel model;
	List courseList = new ArrayList();
	List gender = new ArrayList();

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if (model.getUserName() != "") {
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	@Override
	public LoginModel getModel() {
		// TODO Auto-generated method stub
		model = new LoginModel();
		return model;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if ("".equals(model.getUserName())) {
			addFieldError("studentName", getText("userName"));
		}

		if ("".equals(model.getPassword())) {
			addFieldError("roll", getText("password"));
		}
		super.validate();
	}
}

After that write a xml file for server side validation. The naming convension for writing this aml file is it should start with the class class name for which it is being writtem-validation.xml. Example for Login Action class it should be Login-validation.xml.

LoginValidation-validation.xml

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="userName">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>

<field name="password">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>

</validators>

Then Write a properties file for generating error message on the page.

LoginValidation.propertiesADS_TO_REPLACE_3

userName = Please Enter Name
password = Please Specify Roll No
requiredstring = ${getText(fieldName)} is required

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />

<package name="roseindia" namespace="/" extends="struts-default">

<action name="registrationForm" class="net.roseindia.model.LoginModel"
method="displayForm">
<result name="input">resources/registration.jsp</result>
</action>

<action name="register" class="net.roseindia.action.LoginValidation">
<result name="success">resources/home.jsp</result>
<result name="input">resources/registration.jsp</result>
</action>

</package>

</struts>


When you run this application it will display message as shown below:


 
 
 

Download Select Source Code

Related Tags for Server side Validation :

Advertisements

Ads

Ads

 
Advertisement null

Ads